import { CommandDialog, pickedLineData } from '@amcax/base';
import i18n from '@app-cad/common/utils/i18next';
import { NMTopoBaseCommand } from '@app-cad/environments/nextmesh/types/command';
import { PickInputForFocus } from '@app-cad/common/components/command-form/auto-focus/pick-input';
import { CommandFormWrapper } from '@app-cad/common/components/command-form';
import { GenerateMode, TopoDialogProps } from '../type';
import { useState } from 'react';
import { CommandFormFooter } from '@app-cad/common/components/command-form/footer';
import { documentV2 } from '@app-nextmesh/domain';

import { useCommand } from '@app-base/common/hooks/useCommand';
import { PickInputValueType } from '@app-base/common/components/pick-input/types';

function TopoSweepDialog({ title, onClose }: TopoDialogProps) {
  const [sweepEdgeData, setSweepEdgeData] = useState<pickedLineData[]>([]);
  const [sweepPathData, setSweepPathData] = useState<pickedLineData[]>([]);
  useCommand('cmdTopoSweep', {
    onClose,
  });
  const onSweepEdgeData = (data) => {
    setSweepEdgeData(data);
  };
  const onSweepPathData = (data) => {
    setSweepPathData(data);
  };
  const handleSubmit = async () => {
    const params = {
      socuce: [sweepEdgeData[0].edgeId],
      relyOn: [sweepPathData[0].edgeId],
      mode: GenerateMode.Sweep,
    };
    await documentV2.generateEntity(params);
    onClose();
  };
  const isValid = () => {
    return sweepEdgeData.length > 0 && sweepPathData.length > 0;
  };
  return (
    <CommandDialog
      title={title}
      placement='topLeft'
      onClose={onClose}>
      <CommandFormWrapper>
        <PickInputForFocus
          autoFocus
          controlled
          allowPickType={[PickInputValueType.Line, PickInputValueType.Curve]}
          onPick={onSweepEdgeData}
          value={sweepEdgeData}
          placeholder='请选择扫掠的边'
        />
        <PickInputForFocus
          allowPickType={[PickInputValueType.Line, PickInputValueType.Curve]}
          controlled
          onPick={onSweepPathData}
          value={sweepPathData}
          placeholder='请选择扫掠路径'
        />

        <CommandFormFooter
          onClose={onClose}
          onSubmit={handleSubmit}
          submitText='确定'
          isSubmitDisabled={!isValid()}
        />
      </CommandFormWrapper>
    </CommandDialog>
  );
}
export const cmdTopoSweep: NMTopoBaseCommand = {
  id: 'cmdTopoSweep',
  icon: 'commands/curved/point',
  label: 'command.topo.sweep',
  create: (label: string, onClose: () => void, props) => (
    <TopoSweepDialog
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
